package util;

import model.Coordinates;

public class GeoUtils {
    private static final double EARTH_RADIUS_KM = 6371.0;

    public static double haversine(Coordinates c1, Coordinates c2) {
        double lat1 = Math.toRadians(c1.getLat());
        double lon1 = Math.toRadians(c1.getLng());
        double lat2 = Math.toRadians(c2.getLat());
        double lon2 = Math.toRadians(c2.getLng());

        double dLat = lat2 - lat1;
        double dLon = lon2 - lon1;

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(lat1) * Math.cos(lat2) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return EARTH_RADIUS_KM * c;
    }

    public static int getTravelTimeMinutes(Coordinates from, Coordinates to, double speedKmH) {
        double distance = haversine(from, to);
        double hours = distance / speedKmH;
        return (int) (hours * 60);
    }
}